package com.ruoyi.business.controller;


import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.business.domain.DeliveryBillentryHand;
import com.ruoyi.business.domain.ReceivingBillentryHand;
import com.ruoyi.business.domain.ReceivingBillentryPound;
import com.ruoyi.business.service.ReceivingBillentryHandService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 收货物料明细人工核验Controller
 *
 * @author JerryLu
 * @date 2024/8/29 18:15
 * @return
 */
@RestController
@RequestMapping("/system/receiving_billentry_hand")
public class ReceivingBillentryHandController extends BaseController {
    @Autowired
    private ReceivingBillentryHandService handService;

    /**
     * 查询收货物料明细列表
     */
    @PreAuthorize("@ss.hasPermi('system:receiving_billentry_hand:list')")
    @GetMapping("/list")
    public TableDataInfo list(ReceivingBillentryHand billentryHand) {
        startPage();
        List<ReceivingBillentryHand> list = handService.queryList(billentryHand);
        return getDataTable(list);
    }

    /**
     * 导出收货物料明细列表
     */
    @PreAuthorize("@ss.hasPermi('system:receiving_billentry_hand:export')")
    @Log(title = "收货物料明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ReceivingBillentryHand billentryHand) {
        List<ReceivingBillentryHand> list = handService.queryList(billentryHand);
        ExcelUtil<ReceivingBillentryHand> util = new ExcelUtil<ReceivingBillentryHand>(ReceivingBillentryHand.class);
        util.exportExcel(response, list, "收货物料明细人工核验数据");
    }

    /**
     * 获取收货物料明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:receiving_billentry_hand:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(handService.getById(id));
    }

    /**
     * 获取收货物料明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:note:index')")
    @GetMapping(value = "/receiving/{receivingId}")
    public AjaxResult getReceiptInfo(@PathVariable("receivingId") String receivingId) {
        QueryWrapper<ReceivingBillentryHand> query = Wrappers.query();
        query.eq("receiving_id", receivingId);
        List<ReceivingBillentryHand> list = handService.list(query);
        return success(list);
    }

    /**
     * 新增收货物料明细
     */
    @PreAuthorize("@ss.hasPermi('system:receiving_billentry_hand:add')")
    @Log(title = "收货物料明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ReceivingBillentryHand billentryHand) {
        return toAjax(handService.save(billentryHand));
    }

    /**
     * 修改收货物料明细
     */
    @PreAuthorize("@ss.hasPermi('system:receiving_billentry_hand:edit')")
    @Log(title = "收货物料明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JSONObject json) {
        try {
            Long id = json.getLong("id");
            if (json.getString("form") != null) {
                List<ReceivingBillentryHand> arr = JSON.parseArray(json.getString("form"), ReceivingBillentryHand.class);
                for (ReceivingBillentryHand hand : arr) {
                    hand.setReceivingId(id);
                    hand.setUpdateTime(DateUtils.getNowDate());
                    handService.updateById(hand);
                }
            }
            return success();
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 删除收货物料明细
     */
    @PreAuthorize("@ss.hasPermi('system:receiving_billentry_hand:remove')")
    @Log(title = "收货物料明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(handService.removeByIds(Arrays.asList(ids)));
    }
}

